package main

import (
	"flag"
	"fmt"
	"image"
	"image/gif"
	_ "image/gif"
	"image/jpeg"
	_ "image/jpeg"
	"image/png"
	_ "image/png"
	"os"
	"path/filepath"
	"strings"
)

var pathUrl = flag.String("path", "", "please input image path")

var suffix = flag.String("suffix", "png", "please input image suffix")

func main() {
	flag.Parse()
	originFile, err := os.Open(*pathUrl)
	if err != nil {
		panic(err)
	}
	// 获取 原始文件名称
	base := strings.TrimSuffix(filepath.Base(*pathUrl), filepath.Ext(*pathUrl))
	// 拼接 转换的文件名称
	targetFile, err := os.Create(fmt.Sprintf("%s.%s", base, *suffix))
	if err != nil {
		panic(err)
	}
	// 解析原始图片
	originImg, _, err := image.Decode(originFile)
	if err != nil {
		panic(err)
	}
	switch *suffix {
	case "png":
		err := png.Encode(targetFile, originImg)
		if err != nil {
			panic(err)
		}
		break
	case "gif":
		err := gif.Encode(targetFile, originImg, &gif.Options{})
		if err != nil {
			print(err)
		}
		break
	case "jpeg":
		err := jpeg.Encode(targetFile, originImg, &jpeg.Options{})
		if err != nil {
			panic(err)
		}
		break
	}

}
